﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using FluentHtmlHelpers.Models;

namespace FluentHtmlHelpers.Controllers
{
    public class OrderController : Controller
    {
        //
        // GET: /Order/

        public ActionResult Index()
        {
            var order = new Order()
                {
                    Email = "dauh@softwaremind.pl",
                    Total = 34
                };
            var list = new List<Order>() {order};
            return View(list);
        }

        [HttpPost]
        public ActionResult Update(Order order)
        {
            if (ModelState.IsValid)
            {
                //update repo
            }
        

            return RedirectToAction("Index");
        }


        // GET: /Order/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            var order = new Order()
            {
                Email = "dauh@softwaremind.pl",
                Total = 34
            };
            return View(order);
        }


        // GET: /Order/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            var order = new Order()
            {
                Email = "dauhsoftwaremind.pl",
                Total = 34
            };
            
           // Order order = db.Orders.Find(id);
            //if (order == null)
            //{
            //    return HttpNotFound();
            //}
            return View(order);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "OrderId,OrderDate,Username,FirstName,LastName,Address,City,State,PostalCode,Country,Phone,Email,Total")] Order order)
        {
            if (ModelState.IsValid)
            {
                
                return RedirectToAction("Index");
            }

            return View(order);
        }


        // GET: /Order/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            //Order order = db.Orders.Find(id);
            //if (order == null)
            //{
            //    return HttpNotFound();
            //}
            var order = new Order()
            {
                Email = "dauh@softwaremind.pl",
                Total = 34
            };
            return View(order);
        }

        // POST: /Order/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            var order = new Order()
            {
                Email = "dauh@softwaremind.pl",
                Total = 34
            };
            return RedirectToAction("Index");
        }

        [HttpPost]
        //[ValidateAntiForgeryToken]
        public JsonResult QuickSearch(string term)
        {
            var names = new List<string>() {"name1","name2","name3"};
            return Json(names, JsonRequestBehavior.AllowGet);
        }


        public ActionResult Autocomplete(string term)
        {
            var items = new[] { "Apple", "Pear", "Banana", "Pineapple", "Peach" };

            var filteredItems = items.Where(
                item => item.IndexOf(term, StringComparison.InvariantCultureIgnoreCase) >= 0
                );
            return Json(filteredItems, JsonRequestBehavior.AllowGet);
        }

    }
}
